package com.lvm.app.file;


import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TopicFileSum {
    /**
     * 通过文件目录进行文件扫描
     * @return
     */
    public List<TopicFileInfo>  fileDirIterator(File file){
        List<TopicFileInfo> res = new ArrayList<>();
        if(file.isDirectory()){//如果是目录的话
            int childVideoNumber = FileTypeCheck.childVideoNumber(file);
            int childPicNumber = FileTypeCheck.childPicNumber(file);
            File readme = readmeFile(file);
            if(childVideoNumber>0||childPicNumber>1){
                TopicFileInfo curTopic = new TopicFileInfo();
                String fileName = FileUtil.getInstance().getName(file);
                if(file.getName().length()>fileName.length()){
                    curTopic.setTitle(file.getName());
                }else{
                    curTopic.setTitle(fileName);
                }
                curTopic.setFileDir(file.getName());
                curTopic.setRootPath(file.getPath());
                if(file.getName().toLowerCase().indexOf("sum")>=0){
                    curTopic.getVideoFiles().addAll(allVideoFile(file));
                    curTopic.getPicFiles().addAll(allPicFile(file));
                    curTopic.setReadme(readme);
                    res.add(curTopic);
                    return res;
                }else{
                    if(childVideoNumber==1){//如果目录下面只有一个视频
                        curTopic.setTopicTitle(file.getName());
                        curTopic.getVideoFiles().addAll(allVideoFile(file));
                        curTopic.getPicFiles().addAll(allPicFile(file));
                        curTopic.setReadme(readme);
                        res.add(curTopic);
                        return res;
                    }else if(childVideoNumber>1){
                        List<File> s = allVideoFile(file);
                        List<TopicFileInfo> w  = s.stream().map((item)->{
                            TopicFileInfo topicTemp = new TopicFileInfo();
                            String fileName2 = FileUtil.getInstance().getName(item);
                            if(file.getName().length()>fileName2.length()){
                                topicTemp.setTitle(file.getName());
                            }else{
                                topicTemp.setTitle(fileName2);
                            }
                            topicTemp.setTitle(FileUtil.getInstance().getName(item));
                            topicTemp.setFileDir(FileUtil.getInstance().getName(item));
                            topicTemp.setRootPath(item.getPath());
                            topicTemp.getVideoFiles().add(item);
                            curTopic.setReadme(readme);
                            return topicTemp;
                        }).collect(Collectors.toList());
                        res.addAll(w);
                        return res;
                    }else if(childVideoNumber==0&&childPicNumber>1){
                        curTopic.getPicFiles().addAll(allPicFile(file));
                        curTopic.setReadme(readme);
                        res.add(curTopic);
                        return res;
                    }
                }
            }else if(childVideoNumber==0){
                if(file.listFiles()!=null){
                    for(File chileDir:file.listFiles()){
                        List<TopicFileInfo> childTopic = fileDirIterator(chileDir);
                        res.addAll(childTopic);
                    }
                }

            }

        }
        return res;
    }
    public File readmeFile(File file){
        if(file.isDirectory()){
            File[] f = file.listFiles((s,name)->{
                if(name.indexOf("info")>=0){
                    return true;
                }
                return false;
            });
            if(f!=null&&f.length>0){
                return f[0];
            }
        }
        return null;
    }
    public List<File> allPicFile(File parentFile){
        File fileDir = null;
        List<File> files = new ArrayList<>();
        if(!parentFile.isDirectory()){
            fileDir =  parentFile.getParentFile();
        }else{
            fileDir =  parentFile;
        }
        if(fileDir.listFiles()!=null){
            for(File s: fileDir.listFiles()){
                if(s.isDirectory()){
                    if(s.getName().indexOf("picture")>=0||s.getName().indexOf("图片")>=0){
                        List<File> childFile = allPicFile(s);
                        files.addAll(childFile);
                    }
                }
            }
        }

        File[] filesCurDir = fileDir.listFiles((file,name)->{
            if(FileTypeCheck.checkFileType(new File(name))==FileType.PICTURE){
                return true;
            }else{
                return false;
            }
        });
        if(filesCurDir!=null){
            files.addAll(Arrays.asList(filesCurDir));
        }
        return files;
    }
    public List<File> allVideoFile(File parentFile){
        File fileDir = null;
        List<File> files = new ArrayList<>();
        if(!parentFile.isDirectory()){
            fileDir =  parentFile.getParentFile();
        }else{
            fileDir =  parentFile;
        }
        if(fileDir.listFiles()!=null){
            for(File s: fileDir.listFiles()){
                if(s.isDirectory()){
                    if(s.getName().indexOf("视频")>=0||s.getName().indexOf("video")>=0){
                        List<File> childFile = allVideoFile(s);
                        files.addAll(childFile);
                    }
                }
            }
        }

        File[] filesCurDir = fileDir.listFiles((file,name)->{
            if(FileTypeCheck.checkFileType(new File(name))==FileType.VIDEO){
                return true;
            }else{
                return false;
            }
        });
        if(filesCurDir!=null){
            files.addAll(Arrays.asList(filesCurDir));
        }
        return files;
    }


    public static void main(String[] args) throws IOException {
        TopicFileSum s = new TopicFileSum();
        List<TopicFileInfo> res= s.fileDirIterator(new File("D:\\lvm3.5"));
        Logger logger = LoggerFactory.getLogger(TopicFileSum.class);
//        String json = JSONObject.toJSONString(res);
//        FileWriter fw = new FileWriter("D:\\log.text", true);
//        BufferedWriter writer = new BufferedWriter(fw);
//        writer.write(json);
//        fw.close();
//        fw.close();
    }
}
